package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.utils.MD5Util;
import com.bq.gametrade.utils.NetworkUtils;
import com.bq.gametrade.widget.PayPwdEditText;

import java.util.HashMap;
import java.util.Map;


public class SetPayPasswordActivity extends BaseActivity implements View.OnClickListener {

    private ImageView back, ivService;
    private TextView tvTitle;
    private PayPwdEditText payPwdFirst;
    private PayPwdEditText payPwdConfirm;
    private Button btnConfirmPassword;

    private String firstPayPassword;
    private String operaType = "0";
    private String oldPayPassword;

    private Activity activity;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_paypassword);
        activity = this;
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        if (getIntent().getStringExtra("operaType") != null) {
            operaType = getIntent().getStringExtra("operaType");
            oldPayPassword = getIntent().getStringExtra("oldPayPassword");
        } else {
            operaType = "0";
            oldPayPassword = "1";
        }
        initView();
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        ivService = serchViewById(R.id.iv_userService);
        ivService.setVisibility(View.GONE);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("设置支付密码");

        payPwdFirst = (PayPwdEditText) findViewById(R.id.ppe_pwdFirst);
        payPwdConfirm = (PayPwdEditText) findViewById(R.id.ppe_pwdOk);

        payPwdFirst.initStyle(R.drawable.paypassword_edit_num_bg, 6, 1f, R.color.gray, R.color.black, 25);
        payPwdFirst.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {
                firstPayPassword = str;
                toastShow("请再次输入以确认");

            }
        });

        payPwdConfirm.initStyle(R.drawable.paypassword_edit_num_bg, 6, 1f, R.color.gray, R.color.black, 20);
        payPwdConfirm.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {

                if (firstPayPassword.equals(str)) {
                    if (operaType.equals("0")) {
                        //设置支付密码
                        if(NetworkUtils.isNetworkAvailable(activity)){
                            setPayPassword(MD5Util.md5(str), operaType, "0");
                        }else {
                            toastShow("你还没有连接网络哦");
                        }
                    } else if (operaType.equals("1")) {
                        //修改
//                        if(firstPayPassword.equals(str)){
//                            toastShow("新支付密码不能和旧支付密码一样");
//                            return;
//                        }
                        if(NetworkUtils.isNetworkAvailable(activity)){
                            setPayPassword(MD5Util.md5(str), operaType, oldPayPassword);
                        }else {
                            toastShow("你还没有连接网络哦");
                        }

                    }

                } else {
                    toastShow("两次支付密码输入不一致");
                    payPwdConfirm.clearText();
                }
            }
        });

        btnConfirmPassword = serchViewById(R.id.btn_confirmPayPassword);
        btnConfirmPassword.setOnClickListener(this);
        btnConfirmPassword.setVisibility(View.GONE);
    }


    /***
     * @param payPassword
     * @param type        0：设置   1：修改
     */
    private void setPayPassword(String payPassword, String type, String oldPayPassword) {
        Map<String, String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("pay_pwd", payPassword);
        params.put("update_type", type);
        params.put("old_pwd", oldPayPassword);
        showSpotsDialog("正在设置支付密码···");
        VRHttp.sendRequest(SetPayPasswordActivity.this, HttpLink.SETTING_PAYPASSWORD_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean sCache) {
                closeSpotsDialog();
                if(response != null){
                    ResponseBean bean = (ResponseBean)response;
                    if(bean.status.equals("0")){
                        toastShow("恭喜你！支付密码设置成");
                        application.getUserInfo().pay_pass = "1";
                        finish();
                    }else if(bean.status.equals("99")){
                        toastShow("登录超时请重新登录再试");
                        startActivity(new Intent(SetPayPasswordActivity.this,LoginActivity.class));
                        finish();
                    }else {
                        toastShow("设置支付密码失败！");
                    }
                }else {
                    toastShow("设置支付密码失败");
                }

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
                toastShow("设置支付密码失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });


    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.btn_confirmPayPassword:
                if (TextUtils.isEmpty(payPwdFirst.getPwdText().toString())) {
                    toastShow("请输入支付密码");
                    return;
                }
                if (TextUtils.isEmpty(payPwdConfirm.getPwdText().toString())) {
                    toastShow("请再次确认支付密码");
                    return;
                }
                toastShow("正在提交·····");
                break;
        }
    }
}
